import { ref, computed } from "vue";
import { defineStore } from "pinia";

export const useTokenStore = defineStore(
  "token",
  () => {
    const token = ref(null);
    const userId = ref(null);
    const userInfo = ref(null);
    const getUserInfo = () => {
      return userInfo.value;
    };
    const setUserInfo = (value) => {
      userInfo.value = value;
    };
    const removeUserInfo = () => {
      userInfo.value = null;
    };
    const getToken = () => {
      return token.value;
    };
    const setToken = (value) => {
      token.value = value;
    };
    const removeToken = () => {
      token.value = null;
    };
    const getUserId = () => {
      return userId.value;
    };
    const setUserId = (value) => {
      userId.value = value;
    };
    const removeUserId = () => {
      userId.value = null;
    };
    return {
      userId,
      token,
      userInfo,
      removeUserInfo,
      setUserInfo,
      getUserInfo,
      getToken,
      setToken,
      removeToken,
      getUserId,
      setUserId,
      removeUserId,
    };
  },
  {
    unistorage: true, // 开启后对 state 的数据读写都将持久化
  }
);
